<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use app\common\model\Config as ConfigModel;

class Config extends BaseController
{
    protected $middleware = [
        \app\admin\middleware\LoginAuth::class
    ];

    public function index()
    {
        $Menu = [
            'name' => '网站配置',
            'title' => '网站配置'
        ];
        // 查询配置表所有数据
        $ConfigList = ConfigModel::select();
        // 是否有post提交
        if ($this->request->isPost()) {
            // 接收所有参数
            $params = $this->request->param();
            // 通过key获取id
            $keyList = array_keys($params);
            // 查询配置表的id以及key字段
            $ConfigList = ConfigModel::column('id,key');
            // halt($keyList);
            // 更新的值放到该数组
            $ConfigData = [];
            // 遍历封装数据结构
            foreach ($ConfigList as $key => $item) {
                // halt($item);
                // 判断$keyList是否存在$item['key']  $item['key']=>数组索引
                if (in_array($item['key'], $keyList)) {
                    $ConfigData[] = [
                        'id' => $item['id'],
                        'value' => $params[$item['key']]
                    ];
                }
            }
            if (!empty($_FILES)) {
                // 空的数组
                $FILES = [];

                // 遍历上传的数组里面是否有空的元素
                foreach ($_FILES as $key => $value) {

                    // 跳出循环
                    if ($value['error'] != 0 && $value['size'] <= 0) {
                        continue;
                    }

                    // 不为空的元素封装新的数组
                    $FILES[$key] = $value;
                }

                // 获取新的数组的key => 索引
                $fileKey = array_keys($FILES);

                foreach ($ConfigList as $key => $item) {
                    if (in_array($item['key'], $fileKey)) {
                        $file = ConfigModel::find($item['id']);

                        $fileData[] = $file;

                        $File = upload($item['key']);

                        $FileData[] = $File;

                        if ($File['code'] === 0) {
                            $this->error($File['msg']);
                        }

                        $ConfigData[] = [
                            'id' => $item['id'],
                            'value' => $File['data']
                        ];
                    }
                }
            }

            $ConfigModel = new ConfigModel();

            $result = $ConfigModel->saveAll($ConfigData);

            if ($result !== FALSE) {
                if (!empty($_FILES) && !empty($fileData)) {
                    foreach ($fileData as $key => $value) {
                        $Value =ltrim($value['value'],'/');
                        @is_file((string)$Value) && unlink($Value);
                    }
                }
                $this->success('更新网站配置成功');
            } else {
                if (!empty($_FILES) && !empty($FileData)) {
                    foreach ($FileData as $key => $value) {
                        $Value =ltrim($value['data'],'/');
                        @is_file((string)$Value) && unlink($Value);
                    }
                    $this->error('更新网站配置失败');
                }
            }
        }

        return view('', [
            'menu' => $Menu,
            'ConfigList' => $ConfigList
        ]);
    }
}
